package com.oha.myutils.services;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

public class ActiveMQService {
	
	private static Logger log4j = Logger.getLogger(ActiveMQService.class);
	
	/**
	 * Envia mensaje
	 * @param xml
	 * @throws BusinessException 
	 */
	public void enviarMensaje(StringBuilder xml, String qName){
		try{
			ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
		    Connection conn = cf.createConnection();
		    conn.start();
		    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		    Destination destination = session.createQueue(qName);
		    MessageProducer producer = session.createProducer(destination);
		    TextMessage message = session.createTextMessage(xml.toString());
		    producer.send(message);
		    conn.close();
		}catch(Exception e){
			log4j.error("Exception.", e);
		}
	}
	
}
